package cn.kxm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.kxm.pojo.PurchaseOrderBill;
import cn.kxm.pojo.PurchaseOrderBillDetail;
import cn.kxm.service.PartService;
import cn.kxm.service.PurchaseOrderBillDetailService;
import cn.kxm.service.PurchaseOrderBillService;
import cn.kxm.util.MakeNewCode;

@Controller
@RequestMapping("/PurchaseOrderBill")
public class PurchaseOrderBillController extends BaseController{

	@Autowired
	private PurchaseOrderBillService purchaseOrderBillService;
	@Autowired
	private  MakeNewCode makeNewCode;
	@Autowired
	private PurchaseOrderBillDetailService purchaseOrderBillDetailService;
	@Autowired
	private PartService partService;
	
	@RequestMapping("getall")
	@ResponseBody
	public Object getAll(Integer page,Integer rows,PurchaseOrderBill purchaseOrderBill){
		List<PurchaseOrderBill>list=purchaseOrderBillService.getAll(purchaseOrderBill, page, rows);
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("total", purchaseOrderBillService.getPurchaseOrderBillCount(purchaseOrderBill));
		map.put("rows", list);
		if(list!=null){
			return JSON.toJSON(map);
		}
		return null;
	}
	@RequestMapping("getByCode")
	public String getByCode(Model model,String code){
		model.addAttribute("list",purchaseOrderBillService.getByCode(code));
		return "web/goods/addOrderBillNoUpdate";
	}
	@RequestMapping("getByCodeUpdate")
	public String getByCodeUpdate(Model model,String code,Integer num){
		model.addAttribute("list",purchaseOrderBillService.getByCode(code));
		if(num==1){
			return "web/goods/OrderBillUpdate";
		}else {
			return "web/goods/OrderBillUpdateCheak";
		}
	}
	
	@RequestMapping("makenewcode")
    @ResponseBody
    public Object makeNewCode(String prefix){
		Map<String,Object> map=new HashMap<String, Object>();
    	String code=makeNewCode.getRandomCodeByPrefix(prefix);
    	map.put("result",code);
    	return JSON.toJSONString(map);
    }
	
	@RequestMapping("addBill_Detail")
	@ResponseBody
	public Object addbill_detail(String data,PurchaseOrderBill purchaseOrderBill,Integer number){
		boolean flag=false;
		boolean flag1=false;
		if(purchaseOrderBill!=null && number!=null){
			System.out.println(number);
			purchaseOrderBill.setCheckStatus(number);
			purchaseOrderBill.setPayStatus(1);
			purchaseOrderBill.setInstorageStatus(1);
			int b=purchaseOrderBillService.addPurchaseOrderBillCount(purchaseOrderBill);
			if(b>0){
				flag=true;
			}
		}
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		if(list!=null){
			for(int i=0;i<list.size()-1;i++){
				Map<String, Object> m=list.get(i);
				PurchaseOrderBillDetail p=new PurchaseOrderBillDetail();
				p.setAmount(Integer.parseInt(m.get("amount").toString()));
				p.setPartCode(m.get("code").toString());
				p.setPrice(Double.parseDouble(m.get("price").toString()));
				p.setBillCode(purchaseOrderBill.getCode());
				if(m.get("memo")!=null){
				    p.setMemo(m.get("memo").toString());
				}
				int a=purchaseOrderBillDetailService.addPurchaseOrderBillDetail(p);
				if(a>0){
					flag1=true;
				}
			}
		}
		if(flag1 && flag){
			return JSON.toJSONString("y");
		}else {
			return JSON.toJSONString("n");
		}
	}
	/*
	 * 修改订单
	 */
	@RequestMapping("updatePOB")
	@ResponseBody
	public Object updatePOB(String data,PurchaseOrderBill purchaseOrderBill){
		boolean flag=false;
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		if(list!=null){
			//根据code删除订单明细中所有的数据
			int b=purchaseOrderBillService.modifyPurchaseOrderBill(purchaseOrderBill);
			int num=purchaseOrderBillDetailService.delPurchaseOrderBillDetail(purchaseOrderBill.getCode());
			if(num>0 && b>0){
				for(int i=0;i<list.size()-1;i++){
					Map<String, Object> m=list.get(i);
					PurchaseOrderBillDetail p=new PurchaseOrderBillDetail();
					p.setAmount(Integer.parseInt(m.get("amount").toString()));
					p.setPartCode(m.get("code").toString());
					p.setPrice(Double.parseDouble(m.get("price").toString()));
					p.setBillCode(purchaseOrderBill.getCode());
					if(m.get("pd_memo")!=null){
					   p.setMemo(m.get("pd_memo").toString());
					}
					int a=purchaseOrderBillDetailService.addPurchaseOrderBillDetail(p);
					if(a>0){
						flag=true;
					}else {
						flag=false;
						break;
					}
				}
			}
		}
		if(flag){
			return JSON.toJSONString("y");
		}
		return JSON.toJSONString("n");
	}
	/*
	 * 单删订单
	 */
	@RequestMapping("delBill_Detail")
	@ResponseBody
	public Object delBill_Detail(String code,Integer flag){
		Map<String, Object>map=new HashMap<String, Object>();
		if(flag==2){
			//状态2是已审核不能删除
			map.put("shenhe", "n");
		}else{	
			//状态1是未审核能删除
			map.put("shenhe", "y");
			int b=purchaseOrderBillDetailService.delPurchaseOrderBillDetail(code);
			map.put("zi", b);
			int a=purchaseOrderBillService.delPurchaseOrderBill(code);
			if(a>0 && b>0){
				map.put("flag", "y");
			}else{
				map.put("flag", "n");
			}
		}
		return JSON.toJSON(map);
	}
	/*
	 * 多删订单
	 */
	@RequestMapping("deleteMultiple")
	@ResponseBody
	public Object deleteMultiple(String data){
		boolean flag=false;
		Map<String, Object>map=new HashMap<String, Object>();
		List<Map<String,Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		int sum=0;
		for(int i=0;i<list.size();i++){
			int b=purchaseOrderBillDetailService.delPurchaseOrderBillDetail(list.get(i).get("code").toString());
			int a=purchaseOrderBillService.delPurchaseOrderBill(list.get(i).get("code").toString());
			sum+=b;
			if(a>0 && b>0){
				flag=true;
			}else {
				flag=false;
			}
		}
		if(flag){
			map.put("flag", "y");
			map.put("size",list.size());
			map.put("sum",sum);
		}else{
			map.put("flag", "n");
		}
			
		return JSON.toJSONString(map);		
	}
	@RequestMapping("getPart_Ib")
	@ResponseBody
	public Object getPart_Ib(String data){
		List<Map<String, Object>> list=(List<Map<String, Object>>) JSON.parse(data);
		List<Map<String, Object>>list2=new ArrayList<Map<String,Object>>();
		if(list!=null){
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i).get("code").toString());
				List<Map<String, Object>> list3=partService.getPart_ib(list.get(i).get("code").toString());
				list2.addAll(list3);
			}
		}
		return JSON.toJSON(list2);
	}
	
	@RequestMapping("modifyPurchaseOrderBillByCode")
	@ResponseBody
	public Object modifyPurchaseOrderBillByCode(String code,Integer checkStatus){
		int i=purchaseOrderBillService.modifyPurchaseOrderBillByCode_checkStatus(code, checkStatus);
		Map<String, Object>map=new HashMap<String, Object>();
		if(i>0){
			map.put("flag","y");
			map.put("code", code);
			map.put("checkStatus", checkStatus);
		}
		return JSON.toJSONString(map);
	}
}
